var redis=require("redis");
var unirest = require('unirest');

var cliente=redis.createClient(6379,"192.168.125.85", {});

var processMessage= function(message, author, date){

	

	//unirest.post('https://api.monkeylearn.com/api/v1/categorizer/cl_kQMLDvZY/classify_text/sandbox/')
	unirest.post('https://api.monkeylearn.com/api/v1/categorizer/cl_WiBuWoRy/classify_text/sandbox/')
		.headers({ 'Authorization': 'Token fa000623acf226ba34e690a4f80cc7e5e5323725'})
		.send({ "text": message })
		.end(function (response) {

  			var messageJSON={}
  			messageJSON.probability=0;
  			messageJSON.category= "";
  			messageJSON.author=author;
  			messageJSON.date=date
  			messageJSON.message=message;  			

  			for (var i = 0; i < response.body.result.length; i++)
  			{
  				//console.log(response.body.result[i]);
  				if (response.body.result[i].probability < 0.5)
  				{
  					break;
  				}

  				messageJSON.category += "/" + response.body.result[i].label;
  				messageJSON.probability=response.body.result[i].probability;
  			};

  			if (messageJSON.category == "")
  			{
  				messageJSON.category="noCategory";
  			}
  			else
  			{
  				messageJSON.category=messageJSON.category.substring(1, messageJSON.category.length);
  			}

  			console.log(messageJSON);

  			cliente.publish('messages', JSON.stringify(messageJSON)); 			

	});
} 

exports.processMessage = processMessage

//processMessage("peñarol inteligencia", "jalvarez@dvelop.com.uy", new Date())